import api from './index';
import { KnowledgeItem } from './knowledge';

export interface SearchParams {
  keyword: string;
  page?: number;
  size?: number;
  categoryId?: string;
  tag?: string;
  fileType?: string;
  dateRange?: [string, string];
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

export interface SearchResponse {
  items: KnowledgeItem[];
  total: number;
  page: number;
  size: number;
  took?: number; // 搜索耗时（毫秒）
}

export interface SearchSuggestion {
  text: string;
  type: 'history' | 'popular' | 'suggestion';
}

// 搜索知识
export const search = (params: SearchParams) => {
  return api.get<any, SearchResponse>('/search', { params });
};

// 获取搜索建议
export const getSearchSuggestions = (keyword: string) => {
  return api.get<any, SearchSuggestion[]>('/search/suggestions', { params: { keyword } });
};

// 获取搜索历史
export const getSearchHistory = (limit: number = 10) => {
  return api.get<any, SearchSuggestion[]>('/search/history', { params: { limit } });
};

// 清除搜索历史
export const clearSearchHistory = () => {
  return api.delete<any, void>('/search/history');
};